package com.jt.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component//交给spring管理bean的ioc di
public class MyInterceptor implements HandlerInterceptor{
    long start ;
    long end ;
    @Override //在方法执行前调用
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        start = System.currentTimeMillis();
        System.out.println("方法要被执行啦~");
        return true;//放行
    }
    @Override //在方法执行后调用
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
        end = System.currentTimeMillis();
        System.out.println("方法执行完啦~");
    }
    @Override //都完成后调用
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
        System.out.println("都干完啦~耗时: "+(end-start)+"ms");
    }
}
